package com.njwb.day11.dvd;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

public class DvdDaoImpls implements DvdDao {
    ArrayList<Dvds> dvdList = new ArrayList<>();
    public  void  moren(){
        dvdList.add(new Dvds("名侦探柯南", null, 1, 0));
        dvdList.add(new Dvds("数码宝贝", null, 1, 4));
        dvdList.add(new Dvds("康熙王朝", "2025-07-31", 2, 7));
        dvdList.add(new Dvds("甄嬛传", "2025-06-30", 2, 3));
        dvdList.add(new Dvds("变形金刚", "2024-01-31", 2, 5));
        dvdList.add(new Dvds("还珠格格", "2025-07-27", 2, 6));

    }

    @Override
    public Boolean add(String name) {
        for (Dvds dvd : dvdList) {
            if (dvd.getDvdName().equals(name)) {

               return false;
            }
        }

            dvdList.add(new Dvds(name, null, 1, 0));
            return true;

    }

    @Override
    public String delete(String name) {
        Iterator<Dvds> iterator = dvdList.iterator();
        while (iterator.hasNext()) {
            Dvds dvd = iterator.next();
            if (dvd.getDvdName().equals(name)) {
                if (dvd.getStatus()==1){
                iterator.remove();
                return name+"删除成功";}
                else{return name+"已借出无法删除";}
            }
        }
        return name+"不存在";
    }

    @Override
    public Dvds select(String name) {
        for (Dvds dvd : dvdList) {
            if (dvd.getDvdName().equals(name)) {
                return dvd;
            }
        }

        return null;

    }

    @Override
    public String loan(String name) {
        Iterator<Dvds> iterator = dvdList.iterator();
        while (iterator.hasNext()) {
            Dvds dvd = iterator.next();
            if (dvd.getDvdName().equals(name)) {
                if (dvd.getStatus() == 1) {
                    dvd.setStatus(2); // 修改为已借出
                    dvd.setCount(dvd.getCount() + 1);
                    dvd.setLendDate(java.time.LocalDate.now().toString());
                    return name+"借出成功！！";
                } else {
                    return name+" 已被借出！！";
                }
            }
        }
        return name+"未被找到 ！";
    }

    @Override
    public String returns(String name) {
        Iterator<Dvds> iterator = dvdList.iterator();
        while (iterator.hasNext()) {
            Dvds dvd = iterator.next();
            if (dvd.getDvdName().equals(name)) {
                if (dvd.getStatus() == 2) {
                    dvd.setStatus(1); // 修改为已借出
                    dvd.setLendDate(null);
                    return name+"归还成功！！";
                } else {
                    return name+"DVD 无需归还！！";
                }
            }
        }
        return "未找到 DVD"+name;
    }

    @Override
    public Map<String,Dvds> showdesc() {
     return null;
    }


}
